package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.HealthMapper;
import com.goods.business.service.HealthService;
import com.goods.common.model.business.Health;
import com.goods.common.vo.business.HealthVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class HealthServiceImpl implements HealthService {

    @Autowired
    private HealthMapper healthMapper;

    //判断今日是否打卡
    @Override
    public HealthVO isReport(Long id) {
        Health health = healthMapper.isReport(id);
        if (null != health){
            HealthVO healthVO = new HealthVO();
            BeanUtils.copyProperties(health,healthVO);
            return healthVO;
        }
        return null;
    }

    //立即打卡
    @Override
    public void report(Health health, Long userId) {
        health.setCreateTime(new Date());
        health.setUserId(userId);
        healthMapper.insertSelective(health);
    }

    //打卡记录
    @Override
    public PageVO<HealthVO> history(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Health> healthList = healthMapper.selectAll();
        List<HealthVO> healthVOList = healthList.stream().map(health -> {
            HealthVO healthVO = new HealthVO();
            BeanUtils.copyProperties(health, healthVO);
            return healthVO;
        }).sorted(Comparator.comparing(HealthVO::getCreateTime)).collect(Collectors.toList());
        PageInfo<Health> pageInfo = new PageInfo<>(healthList);
        return new PageVO<>(pageInfo.getTotal(),healthVOList);
    }
}
